extends Control
@onready var 点数label: Label = %"点数Label"
@onready var y_button: TextureButton = %YButton
@onready var y_label: Label = %YLabel
@onready var y_button_2: TextureButton = %YButton2
@onready var z_button: TextureButton = %ZButton
@onready var z_label: Label = %ZLabel
@onready var z_button_2: TextureButton = %ZButton2
@onready var t_button: TextureButton = %TButton
@onready var t_label: Label = %TLabel
@onready var t_button_2: TextureButton = %TButton2
@onready var j_button: TextureButton = %JButton
@onready var j_label: Label = %JLabel
@onready var j_button_2: TextureButton = %JButton2
@onready var 天赋条v_box_container: VBoxContainer = %"天赋条VBoxContainer"
@onready var button_2: Button = $HBoxContainer/Button2

const 天赋条场景 = preload("res://加点/天赋条.tscn")

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	GameCode.属性变化.connect(刷新显示)
	button_2.disabled=true

func 初始化(_data):
	GameCode.替换天赋()
	GameCode.初始化可用属性点()
	刷新显示()
	显示天赋集()
	
func 退出():
	pass

func 刷新显示():
	点数label.text=str(GameCode.可用属性点)
	y_label.text=str(GameCode.CHR)
	z_label.text=str(GameCode.INT)
	t_label.text=str(GameCode.STR)
	j_label.text=str(GameCode.MNY)
	if GameCode.可用属性点>0:
		button_2.disabled=true
	else:
		button_2.disabled=false

func 显示天赋集():
	var 天赋集=GameCode.天赋集
	for 天赋 in 天赋条v_box_container.get_children():
		天赋条v_box_container.remove_child(天赋)
	
	for 天赋 in 天赋集:
		var 天赋显示=天赋条场景.instantiate()
		天赋条v_box_container.add_child(天赋显示)
		天赋显示.显示天赋(天赋)
		
func _on_y_button_pressed() -> void:
	if GameCode.CHR>0:
		GameCode.可用属性点+=1		
		GameCode.CHR-=1

func _on_y_button_2_pressed() -> void:
	if GameCode.CHR<(GameCode.最大可用属性点*GameCode.属性最大比例) and GameCode.可用属性点>0:		
		GameCode.可用属性点-=1		
		GameCode.CHR+=1

func _on_z_button_pressed() -> void:
	if GameCode.INT>0:
		GameCode.可用属性点+=1		
		GameCode.INT-=1

func _on_z_button_2_pressed() -> void:
	if GameCode.INT<(GameCode.最大可用属性点*GameCode.属性最大比例) and GameCode.可用属性点>0:		
		GameCode.可用属性点-=1		
		GameCode.INT+=1

func _on_t_button_pressed() -> void:
	if GameCode.STR>0:
		GameCode.可用属性点+=1		
		GameCode.STR-=1

func _on_t_button_2_pressed() -> void:
	if GameCode.STR<(GameCode.最大可用属性点*GameCode.属性最大比例) and GameCode.可用属性点>0:		
		GameCode.可用属性点-=1		
		GameCode.STR+=1

func _on_j_button_pressed() -> void:
	if GameCode.MNY>0:
		GameCode.可用属性点+=1		
		GameCode.MNY-=1

func _on_j_button_2_pressed() -> void:
	if GameCode.MNY<(GameCode.最大可用属性点*GameCode.属性最大比例) and GameCode.可用属性点>0:		
		GameCode.可用属性点-=1		
		GameCode.MNY+=1

func _on_button_2_pressed() -> void:
	GameViewManager.切换场景("main")

const 随机属性列表:Array=["CHR","INT","STR","MNY"]
func _on_button_pressed() -> void:
	GameCode.可用属性点=GameCode.最大可用属性点
	GameCode.重置属性()
	while GameCode.可用属性点>0:
		var index=randi_range(0,3)
		var 属性名=随机属性列表[index]
		if 	GameCode.get(属性名)<(GameCode.最大可用属性点*GameCode.属性最大比例):
			GameCode.set(属性名,GameCode.get(属性名)+1)
			GameCode.可用属性点-=1
